package com.android.qjpmc.widget.giide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.android.qjpmc.R;
import com.android.qjpmc.widget.utils.DisplayUtils;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.orhanobut.logger.Logger;

import java.security.MessageDigest;

import androidx.annotation.NonNull;

/**
 * description:glide转圆角图片
 * Created by xsf
 * on 2016.04.15:26
 */


public class CornersHeightTransform extends BitmapTransformation {

    private float height;
    private float width;
    private float radius = DisplayUtils.getDimension(R.dimen.dp_5);

    public CornersHeightTransform(Context context) {

    }

    public CornersHeightTransform(int radius, float width, float height) {
        this.height = height;
        this.width = width;
        this.radius = DisplayUtils.getDimension(radius);
    }

    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        return cornersCrop(pool, toTransform);
    }

    private Bitmap cornersCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, width, height);
        Logger.d("radius" + radius);
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }


    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}
